package org.origin.centre.security.point;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.origin.centre.utils.ResponseUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 无效TOKEN异常类重写
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
public class IAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    @SuppressWarnings("ConstantConditions")
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException {
        if (ex instanceof InvalidBearerTokenException) {
            // 其他凭证过期异常信息
            ResponseUtil.failure(response, HttpStatus.UNAUTHORIZED, "访问失败，凭证已过期");
        } else if (ex != null) {
            // 其他认证异常信息
            ResponseUtil.failure(response, HttpStatus.BAD_REQUEST, ex.getMessage());
        } else if (ex instanceof AuthenticationException) {
            // 未上传token异常 import org.springframework.security.authentication.InsufficientAuthenticationException;
            ResponseUtil.failure(response, HttpStatus.UNAUTHORIZED, ex.getMessage());
        } else {
            // 其他异常信息
            ResponseUtil.failure(response, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
        }
        /*else if (ex instanceof OAuth2AccessDeniedException) {
            // 远程调用返回无访问资源异常信息
            this.responseError(response, HttpStatus.FORBIDDEN, HttpStatus.FORBIDDEN.value(), ex.getMessage());
        } else if (ex instanceof InvalidException) {
            // 远程调用返回异常信息
            this.responseError(response, HttpStatus.FORBIDDEN, HttpStatus.FORBIDDEN.value(), ex.getMessage());
        }*/
    }
}
